<?php

class CareerController extends Yaf_Controller_Abstract
{
    private $careerModel;
    private $sessoin;
    public function init()
    {
        $this->careerModel = new CareerModel();
        $this->session = Yaf_Session::getInstance();
    }
    public function indexAction()
    {
        $career = $this->careerModel->fetchCareer();
        $this->getView()->assign('career',$career);
    }
    public function createAction()
    {
        
    }
    public function modifyAction()
    {
        $career = array();
        $id = intval($this->getRequest()->getParam('id'));
        if ($id)
        {
            $career = $this->careerModel->fetchCareerById($id);
            $career[0]['job_describe'] = explode('~!~', $career[0]['job_describe']);
            $career[0]['job_requirement'] = explode('~!~', $career[0]['job_requirement']);
        }
        $this->getView()->assign('career',$career[0]);
    }
   
    public function insertAction()
    { 
        $title = $this->getRequest()->getPost('title');
        $department = $this->getRequest()->getPost('department');
        $superior = $this->getRequest()->getPost('superior');
        $subordinate = $this->getRequest()->getPost('subordinate');
        $describe = $this->getRequest()->getPost('describe');
        $requirement = $this->getRequest()->getPost('requirement');
        $describe = implode('~!~', $describe);
        $requirement = implode('~!~', $requirement);
        $data = array(
            'title' => $title,
            'department' => $department,
            'superior' => $superior,
            'subordinate' => $subordinate,
            'job_describe' => $describe,
            'job_requirement' => $requirement
        );
        $last_id = $this->careerModel->insertCareer($data);
        $this->forward('index');
        return FALSE;
    }
    
    public function updateAction()
    {
        $id = $this->getRequest()->getPost('id');
        $title = $this->getRequest()->getPost('title');
        $department = $this->getRequest()->getPost('department');
        $superior = $this->getRequest()->getPost('superior');
        $subordinate = $this->getRequest()->getPost('subordinate');
        $describe = $this->getRequest()->getPost('describe');
        $requirement = $this->getRequest()->getPost('requirement');
        $describe = implode('~!~', $describe);
        $requirement = implode('~!~', $requirement);
        $data = array(
            'title' => $title,
            'department' => $department,
            'superior' => $superior,
            'subordinate' => $subordinate,
            'job_describe' => $describe,
            'job_requirement' => $requirement
        );
        $last_id = $this->careerModel->updateCareer($data,array('id'=>$id));
        $this->forward('index');
        return FALSE;
    }
    public function deleteAction()
    {
        $id = intval($this->getRequest()->getParam('id'));
        if ($id)
            $this->careerModel->delete('career',array('id'=>$id));
        $this->forward('index');
        return FALSE;
    }
}
?>
